/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * Represents a collection of {@link Date} objects eg. dates where there's blog entries and provides
 * some static methods for Date comparison.
 *
 * @see {@link foo.bar.blog.web.action.general.CalendarActionBean}
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
public class DateList extends ArrayList<Date> {

    private static final Log LOG = LogFactory.getLog(DateList.class);


    private boolean compareTime = false;

    public DateList(List<Date> dates) {
        for (Date d : dates) {
            add(transform(d, compareTime));
        }
    }

    private static Date transform(Date d, boolean compareTime) {
        if (d == null) { return d; }
        if (!compareTime) {
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            return c.getTime();
        }
        return d;
    }

    private static List<Date> transform(Collection<? extends Date> c, boolean compareTime) {
        List<Date> r = new ArrayList<Date>(c.size());
        for (Date d : c) {
            r.add(transform(d, compareTime));
        }
        return r;
    }


    /**
     * Does this {@Link foo.bar.blog.utils.DateList} contains <code>elem</code>?
     * @param elem
     * @return
     */
    public boolean contains(Date elem) {
        return super.contains(transform((Date)elem, compareTime));
    }

    /**
     * Does <code>d1</code> equals <code>d2</code> and should we compare the time portion as well?
     * @param d1
     * @param d2
     * @param compareTime
     * @return
     */
    public static boolean equals(Date d1, Date d2, boolean compareTime) {
        return transform(d1, compareTime).equals(transform(d2, compareTime));    
    }

    /**
     * Is <code>d1</code> after <code>d2</code> and should we compare the time portion as well.
     * @param d1
     * @param d2
     * @param compareTime
     * @return
     */
    public static boolean after(Date d1, Date d2, boolean compareTime) {
        return transform(d1, compareTime).after(transform(d2, compareTime));
    }

    /**
     * Is <code>d1</code> after <code>d2</code> and should we compare the time portion as well.
     * @param d1
     * @param d2
     * @param compareTime
     * @return
     */
    public static boolean before(Date d1, Date d2, boolean compareTime) {
        return transform(d1, compareTime).before(transform(d2, compareTime));
    }
    
}
